#!/bin/bash
echo "$0 - BEGIN"

# Determine the server and share on which to operate.
SERVER=${1%/*}
SHARE=${1##*/}
echo "SERVER = $SERVER"
echo "SHARE = $SHARE"

# Create the mnt directory in the user home directory:
# Does the mnt directory already exist?
if [ ! -d $HOME/mnt ]; then
  # ?(No) Create it.
   echo "creating $HOME/mnt directory"
   mkdir $HOME/mnt
  # Make sure that this directory is only accessible to the user.
   chmod 700 $HOME/mnt
# ?(END)
fi


# Create a directory in the mnt directory for the server:
# Does the server directory already exist?
if [ ! -d $HOME/mnt/$SERVER ]; then
  # ?(NO) Create it.
   echo "creating $HOME/mnt/$SERVER directory"
   mkdir $HOME/mnt/$SERVER
# ?(END)
fi

# Create a directory in the mnt directory for the share:
# Does the share directory exist?
if [ ! -d $HOME/mnt/$SERVER/$SHARE ]; then
  # ?(NO) Create it.
   echo "$0 - creating $HOME/mnt/$SERVER/$SHARE directory"
   mkdir $HOME/mnt/$SERVER/$SHARE
  # Link the smbh-mount-this script into it.
   ln /usr/bin/smbh-mount-this $HOME/mnt/$SERVER/$SHARE
# ?(END)
fi

# Run the smbh-mount-this script from within the share directory:
# Does the share directory contain the link to the smbh-mount-this script?
if [ -x $HOME/mnt/$SERVER/$SHARE/smbh-mount-this ]; then
  # ?(YES) Run the script. 
  echo "mounting share"
   $HOME/mnt/$SERVER/$SHARE/smbh-mount-this
else
  # ?(NO) The script is already mounted.
   echo "share previously mounted"
# ?(END)
fi

echo "$0 - END"
exit 0
